#!/bin/bash
clear
RED="\033[31m"    # Error message
GREEN="\033[32m"  # Success message
YELLOW="\033[33m" # Warning message
BLUE="\033[36m"   # Info message
RESET='\033[0m'

if [ "$EUID" -ne 0 ]; then
  echo -e "${RED} Anda tiada kebenaran untuk menjalankan skrip ini! ${RESET}"
  exit 1
fi

apt-get -qq update
bash -c "$(curl -L https://github.com/XTLS/Xray-install/raw/main/install-release.sh)" @ install
systemctl stop xray

mkdir /usr/local/etc/xray/clients
touch /usr/local/etc/xray/clients/.accounts

cp /etc/letsencrypt/live/cybertize.ml/fullchain.pem /usr/local/etc/xray/xray.crt
cp /etc/letsencrypt/live/cybertize.ml/privkey.pem /usr/local/etc/xray/xray.key
chmod 644 /usr/local/etc/xray/xray.crt
chmod 644 /usr/local/etc/xray/xray.key

# ipAddr=$(wget -qO- ipv4.icanhazip.com)
xrayCrt='/usr/local/etc/xray/xray.crt'
xrayKey='/usr/local/etc/xray/xray.key'
clientUID='1d716ea0-9670-4359-800b-ed8d783ec04b'
hostAddr=$(cat /usr/local/.environment | grep 'DOMAIN' | cut -d '=' -f 2 | head -n 1)
emailAddr=$(cat /usr/local/.environment | grep 'EMAIL' | cut -d '=' -f 2)

cat >/usr/local/etc/xray/config.json <<-EOF
{
  "log": {
    "loglevel": "warning"
  },
  "inbounds": [
    {
      "port": 1310,
      "listen": "0.0.0.0",
      "protocol": "trojan",
      "settings": {
        "clients": [
          {
            "password": "",
            "level": 0,
            "email": "love@example.com"
          }
        ],
        "fallbacks": [
          {
            "dest": 80
          }
        ]
      },
      "streamSettings": {
        "network": "tcp",
        "security": "none",
        "tcpSettings": {
          "acceptProxyProtocol": true
        }
      }
    },

    {
      "port": 443,
      "protocol": "vless",
      "settings": {
        "clients": [
          {
            "id": "",
            "flow": "xtls-rprx-direct",
            "level": 0,
            "email": "love@example.com"
          }
        ],
        "decryption": "none",
        "fallbacks": [
          {
            "dest": 1310,
            "xver": 1
          },
          {
            "path": "/websocket",
            "dest": 1234,
            "xver": 1
          },
          {
            "path": "/vmesstcp",
            "dest": 2345,
            "xver": 1
          },
          {
            "path": "/vmessws",
            "dest": 3456,
            "xver": 1
          }
        ]
      },
      "streamSettings": {
        "network": "tcp",
        "security": "xtls",
        "xtlsSettings": {
          "alpn": [
            "http/1.1"
          ],
          "certificates": [
            {
              "certificateFile": "/path/to/fullchain.crt",
              "keyFile": "/path/to/private.key"
            }
          ]
        }
      }
    },
    {
      "port": 1234,
      "listen": "0.0.0.0",
      "protocol": "vless",
      "settings": {
        "clients": [
          {
            "id": "",
            "level": 0,
            "email": "love@example.com"
          }
        ],
        "decryption": "none"
      },
      "streamSettings": {
        "network": "ws",
        "security": "none",
        "wsSettings": {
          "acceptProxyProtocol": true,
          "path": "/websocket"
        }
      }
    },
    
    {
      "port": 2345,
      "listen": "0.0.0.0",
      "protocol": "vmess",
      "settings": {
        "clients": [
          {
            "id": "",
            "level": 0,
            "email": "love@example.com"
          }
        ]
      },
      "streamSettings": {
        "network": "tcp",
        "security": "none",
        "tcpSettings": {
          "acceptProxyProtocol": true,
          "header": {
          "type": "http",
            "request": {
              "path": [
                "/vmesstcp"
              ]
            }
          }
        }
      }
    },
    {
      "port": 3456,
      "listen": "0.0.0.0",
      "protocol": "vmess",
      "settings": {
        "clients": [
          {
            "id": "",
            "level": 0,
            "email": "love@example.com"
          }
        ]
      },
      "streamSettings": {
        "network": "ws",
        "security": "none",
        "wsSettings": {
          "acceptProxyProtocol": true,
          "path": "/vmessws"
        }
      }
    }
  ],
  "outbounds": [
    {
      "protocol": "freedom"
    }
  ]
}
EOF
systemctl start xray

echo
echo -e "${GREEN} Pemasangan xray pakej telah selesai. ${RESET}"
echo
